﻿using BLL;
using Inspinia_MVC5.Code;
using Inspinia_MVC5.Models;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class FirstPageController : Controller
    {
        // GET: First
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult SIGN_IMPORTANT(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_SIGN_IMPORTANT_24Service rsi = new R_SIGN_IMPORTANT_24Service();
                var datalist = rsi.FindAll(c=>c.Well_ID.Trim() == wellid).OrderByDescending(c=>c.Time).Take(24);
                if (datalist.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = datalist }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult SIGN_PER_CURR_VOLT_POW(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_SIGN_PER_CURR_VOLT_POW_24Service rsi = new R_SIGN_PER_CURR_VOLT_POW_24Service();
                var datalist = rsi.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).Take(24);
                if (datalist.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = datalist }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult SIGN_INDD(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_SIGN_INDD_24Service rsi = new R_SIGN_INDD_24Service();
                R_SIGN_StructureService rsstruc = new R_SIGN_StructureService();
                var datalist = rsi.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).Take(24);
                var scale_ = rsstruc.Find(c => c.IDCode.Trim() == "MW4095");
                if (datalist.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = datalist, scale = scale_ }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult SIGN_ELECD(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_SIGN_ELECD_24Service rsi = new R_SIGN_ELECD_24Service();
                R_SIGN_StructureService rsstruc = new R_SIGN_StructureService();
                var datalist = rsi.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).Take(24);
                var scale_1 = rsstruc.Find(c => c.IDCode.Trim() == "MW4497");
                var scale_2 = rsstruc.Find(c => c.IDCode.Trim() == "MW4697");
                if (datalist.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = datalist, scale1 = scale_1, scale2 = scale_2 }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult SIGN_Main(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_SIGN_Main_24Service rsm = new R_SIGN_Main_24Service();
                var datalist = rsm.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).Take(24);
                if (datalist.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = datalist }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult ADJU_IMPORTANT(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_ADJU_IMPORTANTService rai = new R_ADJU_IMPORTANTService();
                R_SIGN_AssociatedService rsa = new R_SIGN_AssociatedService();
                var xds = rsa.All();
                R_SIGN_NEWService rsn = new R_SIGN_NEWService();
                R_SIGN_StructureService rsstruc = new R_SIGN_StructureService();
                R_ADJU_StructureService rastruc = new R_ADJU_StructureService();
                var datalist = rai.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).First();
                var rsndata = rsn.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).First();
                var rsaupdata = rsa.FindAll(c => c.Well_ID.Trim() == wellid && c.MW0033 == "1 ").OrderByDescending(c => c.Time).First();
                var rsadowndata = rsa.FindAll(c => c.Well_ID.Trim() == wellid && c.MW0033 == "2 ").OrderByDescending(c => c.Time).First();
                List<R_SIGN_Structure> rsstruc_adju = new List<R_SIGN_Structure>();
                List<R_ADJU_Structure> rastruc_adju = new List<R_ADJU_Structure>();
                rsstruc_adju.Add(rsstruc.Find(c => c.IDCode.Trim() == "MW0006"));
                rsstruc_adju.Add(rsstruc.Find(c => c.IDCode.Trim() == "MW0007"));
                rsstruc_adju.Add(rsstruc.Find(c => c.IDCode.Trim() == "MW0011"));
                rastruc_adju.Add(rastruc.Find(c => c.IDCode.Trim() == "MW1589"));
                rastruc_adju.Add(rastruc.Find(c => c.IDCode.Trim() == "MW1591"));
                if (datalist == null)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = datalist, addition = rsndata, structure = rsstruc_adju, adjustructure = rastruc_adju, updata = rsaupdata, downdata = rsadowndata }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult OtherInfo(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                WellService ws = new WellService();
                int wid = int.Parse(wellid);
                Well datalist = ws.Find(c => c.ID == wid);
                WellModel wm = new WellModel
                {
                    ID = datalist.ID,
                    IP_Port = datalist.IP_Port,
                    Name1 = datalist.Name1,
                    Name2 = datalist.Name2,
                    Width = datalist.Width,
                    Height = datalist.Width,
                    Status = datalist.Width,
                    BLOCK_ID = datalist.BLOCK_ID.Value,
                    Station = datalist.Station,
                    Deep = datalist.Deep,
                    Diameter = datalist.Diameter,
                    Angle = datalist.Angle,
                    TabWidth = datalist.TabWidth,
                    TabHeight = datalist.TabHeight
                };
                if (datalist.Station == null)
                    wm.Station = "暂无_暂无";
                if (wm == null)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = wm }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult INFO_Important_Data(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_INFO_DataService rsm = new R_INFO_DataService();
                R_SIGN_IMPORTANTService rsi = new R_SIGN_IMPORTANTService();
                R_SIGN_AssociatedService rsa = new R_SIGN_AssociatedService();
                var datalist1 = rsm.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).Take(1);
                var datalist2 = rsi.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).Take(1);
                var datalist3 = rsa.FindAll(c => c.Well_ID.Trim() == wellid).OrderByDescending(c => c.Time).Take(1);
                if (datalist1.Count() == 0 || datalist2.Count() == 0 || datalist3.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data1 = datalist1, data2 = datalist2, data3 = datalist3 }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}